import copy

from django.conf import settings
from django.template import Library

register = Library()


@register.inclusion_tag('tags/my_menu.html')
def my_menu(request):
    # 1. 获取用户角色
    role = request.my_user.role
    # 2. 获取角色菜单
    user_menu_list = copy.deepcopy(settings.MY_MENU.get(role))  # 避免直接修改内存中的源文件
    for menu in user_menu_list:
        children = menu.get('menu')
        for child in children:
            # if child.get('url') == request.path_info:  # 版本1
            if child.get('name') == request.my_user.menu_name:
                child.update({'class': 'active'})
    return {'menu_list': user_menu_list}
